package com.zxl.live.screen.screen.ui.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdSize;
import com.zxl.live.screen.R;
import com.zxl.live.screen.screen.constant.ISharedPreferences;
import com.zxl.live.screen.screen.constant.ScreenSetting;
import com.zxl.live.screen.screen.ui.widget.SettingItemView;
import com.zxl.live.screen.ui.activity.MainActivity;
import com.zxl.live.tools.base.BaseAppCompatFragment;
import com.zxl.live.tools.guide.AlertPermissionGuide;
import com.zxl.live.tools.track.StatisticsConstant;
import com.zxl.live.tools.track.StatisticsType;

/**
 * Created by Administrator on 2018/1/9.
 */

public class ScreenSettingFragment extends BaseAppCompatFragment implements View.OnClickListener {

    private boolean mIsJumpAlertPage;

    private SettingItemView mScreenSwitchView;

    private SettingItemView mScreenNotificationSwitchView;

    private SettingItemView mScreenTypeView;

    private SettingItemView mScreenNumberView;

    private AlertPermissionGuide mAlertPermissionGuide;

    private LinearLayout mAdsLayout;

    private AdListener mAdListener = new AdListener() {

        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            MainActivity.mBannerProxy.showAds(mAdsLayout);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_screen_settings, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mScreenSwitchView = (SettingItemView) view.findViewById(R.id.screen_switch);
        mScreenSwitchView.setChecked(ScreenSetting.sIsSwitchOpened);
        mScreenSwitchView.setOnClickListener(this);

        mScreenNotificationSwitchView = (SettingItemView) view.findViewById(R.id.screen_notification_switch);
        mScreenNotificationSwitchView.setChecked(ScreenSetting.sIsNotificationEnabled);
        mScreenNotificationSwitchView.setOnClickListener(this);

        mScreenTypeView = (SettingItemView) view.findViewById(R.id.screen__type);
        mScreenTypeView.setSwitchText(getResources().getStringArray(R.array.efficet_type)[ScreenSetting.sEffciencyType]);
        mScreenTypeView.setOnClickListener(this);

        mScreenNumberView = (SettingItemView) view.findViewById(R.id.screen_number);
        mScreenNumberView.setSwitchText(getResources().getStringArray(R.array.efficet_size)[ScreenSetting.sEffciencySize]);
        mScreenNumberView.setOnClickListener(this);

        mAlertPermissionGuide = new AlertPermissionGuide(getContext(), getActivity().getClass());
        mAdsLayout = (LinearLayout) view.findViewById(R.id.ads_layout);

        MainActivity.mBannerProxy.setProxy(mAdListener);
        if (!MainActivity.mBannerProxy.showAds(mAdsLayout)) {
            MainActivity.mBannerProxy.loadAds(getContext(), AdSize.BANNER, "ca-app-pub-1417586053260517/7260412070");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mAlertPermissionGuide.stopMonitor();
        if (mIsJumpAlertPage && mAlertPermissionGuide.havePermission()) {
            mIsJumpAlertPage = false;
            ScreenSetting.saveLockerSettings(ISharedPreferences.SETTINGS_SCREEN_SWITCH, true);
            mScreenSwitchView.setChecked(ScreenSetting.sIsSwitchOpened);
            StatisticsType.SCREEN_SETTING.upload(getContext(), StatisticsConstant.Event.START_ALERT, "success");
        }
    }

    @Override
    public void onClick(View v) {
        if (v == mScreenSwitchView) {
            boolean checked = mScreenSwitchView.isChecked();
            if (!checked && !mAlertPermissionGuide.havePermission()) {
                mIsJumpAlertPage = true;
                mAlertPermissionGuide.startMonitor();
                StatisticsType.SCREEN_SETTING.upload(getContext(), StatisticsConstant.Event.START_ALERT, "start");
                return;
            }
            ScreenSetting.saveLockerSettings(ISharedPreferences.SETTINGS_SCREEN_SWITCH, !checked);
            mScreenSwitchView.setChecked(ScreenSetting.sIsSwitchOpened);
            StatisticsType.SCREEN_SETTING.upload(getContext(), StatisticsConstant.Event.SWITCH_STATUS, String.valueOf(ScreenSetting.sIsSwitchOpened));
        } else if (v == mScreenNotificationSwitchView) {
            if (mScreenNotificationSwitchView.isChecked()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle(R.string.tips);
                builder.setMessage(R.string.notify_warn);
                builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ScreenSetting.saveLockerSettings(ISharedPreferences.SETTINGS_SCREEN_NOTIFICATION, !mScreenNotificationSwitchView.isChecked());
                        mScreenNotificationSwitchView.setChecked(ScreenSetting.sIsNotificationEnabled);
                        StatisticsType.SCREEN_SETTING.upload(getContext(), StatisticsConstant.Event.NOTIFICATION_STATUS, "ok_dialog");
                        StatisticsType.SCREEN_SETTING.upload(getContext(), StatisticsConstant.Event.NOTIFICATION_STATUS, String.valueOf(ScreenSetting.sIsNotificationEnabled));
                    }
                });
                builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StatisticsType.SCREEN_SETTING.upload(getContext(), StatisticsConstant.Event.NOTIFICATION_STATUS, "cancel_dialog");
                    }
                });
                builder.show();
            } else {
                ScreenSetting.saveLockerSettings(ISharedPreferences.SETTINGS_SCREEN_NOTIFICATION, !mScreenNotificationSwitchView.isChecked());
                mScreenNotificationSwitchView.setChecked(ScreenSetting.sIsNotificationEnabled);
                StatisticsType.SCREEN_SETTING.upload(getContext(), StatisticsConstant.Event.NOTIFICATION_STATUS, String.valueOf(ScreenSetting.sIsNotificationEnabled));
            }
        } else if (v == mScreenTypeView) {
            if (!ScreenSetting.sIsSwitchOpened) {
                Toast.makeText(getContext(), R.string.toast_open_swicth_with_setting, Toast.LENGTH_SHORT).show();
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle(R.string.dialog_type_title);
            builder.setSingleChoiceItems(R.array.efficet_type, ScreenSetting.sEffciencyType, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which != ScreenSetting.sEffciencyType) {
                        ScreenSetting.saveLockerSettings(ISharedPreferences.SETTINGS_SCREEN_TYPE, which);
                        String text = getResources().getStringArray(R.array.efficet_type)[ScreenSetting.sEffciencyType];
                        mScreenTypeView.setSwitchText(text);
                        StatisticsType.SCREEN_SETTING.upload(getContext(), StatisticsConstant.Event.TYPE, text);
                    }
                    dialog.dismiss();
                }
            });
            builder.show();
        } else if (v == mScreenNumberView) {
            if (!ScreenSetting.sIsSwitchOpened) {
                Toast.makeText(getContext(), R.string.toast_open_swicth_with_setting, Toast.LENGTH_SHORT).show();
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle(R.string.dialog_number_title);
            builder.setSingleChoiceItems(R.array.efficet_size, ScreenSetting.sEffciencySize, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which != ScreenSetting.sEffciencySize) {
                        ScreenSetting.saveLockerSettings(ISharedPreferences.SETTINGS_SCREEN_SIZE, which);
                        String text = getResources().getStringArray(R.array.efficet_size)[ScreenSetting.sEffciencySize];
                        mScreenNumberView.setSwitchText(text);
                        StatisticsType.SCREEN_SETTING.upload(getContext(), StatisticsConstant.Event.NUMBER, text);
                    }
                    dialog.dismiss();
                }
            });
            builder.show();
        } else {
            onBackPressed();
        }
    }

}
